<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Wp_Header
{
    private static $cssList = array();
    private static $jsList = array();

    /**
     * Add file to cssList or jsList
     * @param string $path
     * @param string|string[] $files
     * @param string $ext
     */
    private function _Add($path, $files, $ext)
    {
        if (!is_string($path)) return;
        if (is_string($files)) $files = array($files);
        if (!is_array($files)) return;
        if ($ext === 'css') {
            foreach ($files as $f) {
                if (!is_string($f)) continue;
                Wp_Header::$cssList[] = $path . $f;
            }
        }
        if ($ext === 'js') {
            foreach ($files as $f) {
                if (!is_string($f)) continue;
                Wp_Header::$jsList[] = $path . $f;
            }
        }
    }

    private function _Load($path, $files, $ext)
    {
        $this->_Add($path, $files, $ext);
        $filesPath = array();
        if ($ext === 'css') {
            $filesPath = Wp_Header::$cssList;
        }
        if ($ext === 'js') {
            $filesPath = Wp_Header::$jsList;
        }
        return $this->_Render($filesPath, $ext);
    }

    private function _Render($filePath, $ext)
    {
        $html = '';
        foreach ($filePath as $file) {
            $fullPath = $file . '.' . $ext;
            $minFile = $file . '.min.' . $ext;
            if (is_file(FCPATH.$minFile)) {
                $fullPath = $minFile;
                goto render;
            }
            if (!is_file(FCPATH.$fullPath)) {
                $html .= "\t<!-- Files Not Found:  {$fullPath}-->\n";;
                continue;
            }
            render:
            $version = filemtime(FCPATH.$fullPath);
            $source = base_url($fullPath);
            if($ext === 'css'){
                $html .= "\t<link rel='stylesheet' type='text/css' media='screen' href='{$source}?v={$version}'/>\n";
            }
            if($ext === 'js'){
                $html .= "\t<script type='text/javascript' src='{$source}?v={$version}'></script>\n";
            }
        }
        return $html;
    }

    /**
     * Add Css files to CSS LIST
     * @param string $patch
     * @param string|string[] $files
     */
    public function AddCss($patch, $files)
    {
        $this->_Add($patch, $files, 'css');
    }

    /**
     * Add Js files to JS LIST
     * @param string $patch
     * @param string|string[] $files
     */
    public function AddJs($patch, $files)
    {
        $this->_Add($patch, $files, 'js');
    }

    /**
     * Load style
     * @param string $path
     * @param string|string[] $files
     * @return string
     */
    public function LoadCss($path, $files)
    {
        return $this->_Load($path,$files,'css');
    }

    /**
     * Load javascript
     * @param string $path
     * @param string|string[] $files
     * @return string
     */
    public function LoadJs($path, $files)
    {
        return $this->_Load($path,$files,'js');
    }
}